//
//  ImgZoom.m
//  优学荟管理系统
//
//  Created by 孔焕宁 on 17/7/6.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "ImgZoom.h"

@implementation ImgZoom
{
    // 图片容器
    UIButton *_ButtonImgView;
    // 图片
    UIImage     *_Img;
    // 当前图片容器
    UIImageView *_CurrentImgView;
}
/** 类实例化 */
+ (instancetype)MgrButtonImgView:(UIButton *)ButtonImgView
{
    return [[self alloc] initWithButtonImgView:ButtonImgView];
}


/** 实例化 */
- (instancetype)initWithButtonImgView:(UIButton *)ButtonImgView
{
    self = [super init];
    if (self)
    {
        _ButtonImgView = ButtonImgView;
        _ButtonImgView.userInteractionEnabled = NO;
        _Img = ButtonImgView.currentBackgroundImage;

        // 设置背景view
        [self ConfigBackView];

        // 1、处理图片的位置，并添加至根视图上
        [self MgrImgFrameAndAddSubview];

        // 2、将图片按比例进行动画放大至全屏
        [self Show];
    }
    return self;
}

/** 设置背景view */
- (void)ConfigBackView
{
    // 1、实例
    self.userInteractionEnabled = YES;
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    self.maximumZoomScale = 2.0;//最大倍率（默认倍率）
    self.minimumZoomScale = 1.0;//最小倍率（默认倍率）
    self.decelerationRate = 1.0;//减速倍率（默认倍率）
    self.delegate = self;
//    [[UIApplication sharedApplication].keyWindow addSubview:self];
//    [[[[UIApplication sharedApplication] delegate] window] addSubview:self];
//    [[UIApplication sharedApplication].keyWindow.window addSubview:self];
    [[UIApplication sharedApplication].windows.firstObject addSubview:self];
    
    // 2、增加单击关闭事件
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Close:)]];
}



/** 处理图片的位置，并添加至根视图上 */
- (void)MgrImgFrameAndAddSubview
{
    // 1、得到这个imgView的绝对坐标
    CGRect ImgRect = [_ButtonImgView convertRect:_ButtonImgView.bounds toView:[UIApplication sharedApplication].keyWindow.rootViewController.view];

    // 2、得到位置之后，新建一个ImgView放到根视图上
    _CurrentImgView = [[UIImageView alloc] initWithFrame:ImgRect];
    _CurrentImgView.image = _Img;
    [self addSubview:_CurrentImgView];
}







#pragma mark - 图片操作方法
/** 将图片按比例进行动画放大至全屏 */
- (void)Show
{
    // 1、获取这张图片是原本宽高,再按比例添加到全屏
    CGSize ImgSize = _Img.size;
    CGFloat Scale = [UIScreen mainScreen].bounds.size.width/ImgSize.width;

    self.userInteractionEnabled = NO;
    
    [UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:5.0 options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
        // 比例全屏
        _CurrentImgView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, ImgSize.height*Scale);
        _CurrentImgView.center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
    } completion:^(BOOL finished) {
        NSLog(@"放大结束");
        self.userInteractionEnabled = YES;
    }];

}

/** 关闭图片 */
- (void)Close:(UITapGestureRecognizer *)Tap
{
    // 1、得到这个imgView的绝对坐标
    CGRect ImgRect = [_ButtonImgView convertRect:_ButtonImgView.bounds toView:[[UIApplication sharedApplication].windows lastObject]];

    // 2、动画关闭并销毁
    self.userInteractionEnabled = NO;
    [UIView animateWithDuration:0.5 animations:^{
        // 比例全屏
        _CurrentImgView.frame = ImgRect;
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    } completion:^(BOOL finished) {
        self.userInteractionEnabled = YES;
        _ButtonImgView.userInteractionEnabled = YES;
        [self removeFromSuperview];
    }];
}

@end
